/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

/** Allows to choose a destinatory */
final class ChooseDest extends JDialog {
	/** Language resource */
	private ResourceBundle msgBundle;

	/** User profile */
	private Profile profile;

	/** Main panel */
	private JPanel panel;

	private JLabel search;

	/** Textfield for the search */
	private JTextField filter;

	private JLabel choiceList;

	/** List filled with entries */
	private JList list;

	/** ScrollPane for the list */
	private JScrollPane listPane;

	private JButton to;
	private JButton cc;
	private JButton bcc;

	private JList toList;
	private JList ccList;
	private JList bccList;

	private JScrollPane toScrollPane;
	private JScrollPane ccScrollPane;
	private JScrollPane bccScrollPane;

	private Vector toNames;
	private Vector ccNames;
	private Vector bccNames;

	private String toEmails[];
	private String ccEmails[];
	private String bccEmails[];

	private JButton ok;
	private JButton cancel;

	private ChooseDestListener listener;

	private Vector names;

	/**
	 * Constructor
	 * 
	 * @param profile
	 *            user profile
	 * @param msgBundle
	 *            language resource
	 */
	protected ChooseDest(Profile profile, ResourceBundle msgBundle) {
		super((JFrame) null, msgBundle.getString("ChooseDest.frameTitle"), true);

		this.msgBundle = msgBundle;

		listener = new ChooseDestListener();

		this.profile = profile;

		names = new Vector();

		panel = new JPanel();

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		panel.setLayout(gridBag);

		Common.buildConstraints(constraints, 0, 0, 1, 1, 40, 10);
		search = new JLabel(msgBundle.getString("ChooseDest.searchLabel"));
		gridBag.setConstraints(search, constraints);
		panel.add(search);

		Common.buildConstraints(constraints, 0, 1, 1, 1, 0, 10);
		filter = new JTextField(20);
		filter.addKeyListener(listener);
		gridBag.setConstraints(filter, constraints);
		panel.add(filter);

		Common.buildConstraints(constraints, 0, 2, 1, 1, 0, 10);
		choiceList = new JLabel(
				msgBundle.getString("ChooseDest.choiceListLabel"));
		gridBag.setConstraints(choiceList, constraints);
		panel.add(choiceList);

		Common.buildConstraints(constraints, 0, 3, 1, 3, 0, 50);

		if (profile.getLdapUser()) {
			names = LdapSearch.getList(profile);

			if (names == null) {
				names = new Vector();
			} else {
				profile.setLdapAddresses(names);
			}
		}

		names.addAll(profile.getAddresses());

		list = new JList(names);

		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listPane = new JScrollPane(list);
		gridBag.setConstraints(listPane, constraints);
		listPane.setPreferredSize(new Dimension(200, 300));
		panel.add(listPane);

		Common.buildConstraints(constraints, 1, 3, 1, 1, 20, 0);
		to = new JButton("TO: ->");
		to.addActionListener(listener);
		gridBag.setConstraints(to, constraints);
		panel.add(to);

		Common.buildConstraints(constraints, 2, 3, 1, 1, 40, 30);
		toNames = new Vector();
		toList = new JList();
		toScrollPane = new JScrollPane(toList);
		gridBag.setConstraints(toScrollPane, constraints);
		panel.add(toScrollPane);

		Common.buildConstraints(constraints, 1, 4, 1, 1, 0, 0);
		cc = new JButton("CC: ->");
		cc.addActionListener(listener);
		gridBag.setConstraints(cc, constraints);
		panel.add(cc);

		Common.buildConstraints(constraints, 2, 4, 1, 1, 40, 30);
		ccNames = new Vector();
		ccList = new JList();
		ccScrollPane = new JScrollPane(ccList);
		gridBag.setConstraints(ccScrollPane, constraints);
		panel.add(ccScrollPane);

		Common.buildConstraints(constraints, 1, 5, 1, 1, 0, 0);
		bcc = new JButton("BCC: ->");
		bcc.addActionListener(listener);
		gridBag.setConstraints(bcc, constraints);
		panel.add(bcc);

		Common.buildConstraints(constraints, 2, 5, 1, 1, 40, 30);
		bccNames = new Vector();
		bccList = new JList();
		bccScrollPane = new JScrollPane(bccList);
		gridBag.setConstraints(bccScrollPane, constraints);
		panel.add(bccScrollPane);

		Common.buildConstraints(constraints, 0, 6, 2, 1, 0, 10);
		ok = new JButton("OK");
		ok.addActionListener(listener);
		gridBag.setConstraints(ok, constraints);
		panel.add(ok);

		Common.buildConstraints(constraints, 2, 6, 2, 1, 0, 10);
		cancel = new JButton(msgBundle.getString("ChooseDest.cancelLabel"));
		cancel.addActionListener(listener);
		gridBag.setConstraints(cancel, constraints);
		panel.add(cancel);

		setContentPane(panel);
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

		setSize(800, 600);
		show();
	}

	/**
	 * Allows to get the TO field's content
	 * 
	 * @return array of names
	 */
	protected final String[] getTO() {
		return (toEmails);
	}

	/**
	 * Allows to get the CC field's content
	 * 
	 * @return array of names
	 */
	protected final String[] getCC() {
		return (ccEmails);
	}

	/**
	 * Allows to get the BCC's field content
	 * 
	 * @return array of names
	 */
	protected final String[] getBCC() {
		return (bccEmails);
	}

	/** Listener for this class */
	private final class ChooseDestListener extends KeyAdapter implements
			ActionListener {
		/**
		 * Method invoked when the user types a key
		 * 
		 * @param e
		 *            keyevent triggered
		 */
		public final void keyReleased(KeyEvent e) {
			String f = filter.getText();

			if (f.compareTo("") != 0) {
				int size = names.size();

				for (int i = 0; i < size; i++) {
					Entry entry = (Entry) names.get(i);
					String name = entry.toString();

					name = name.toLowerCase();
					String fLowered = f.toLowerCase();

					if (name.startsWith(fLowered)) {
						list.setSelectedValue(entry, true);
						break;
					}
				}
			}
		}

		/**
		 * This method is invoked when an event is triggered
		 * 
		 * @param e
		 *            event
		 */
		public final void actionPerformed(ActionEvent e) {
			JButton b = (JButton) e.getSource();

			if (b == to) {
				String name = ((Entry) list.getSelectedValue()).toString();

				if (name != null) {
					toNames.add(name);
					toList.setListData(toNames);
				}

				else
					JOptionPane.showMessageDialog(null, msgBundle
							.getString("ChooseDest.noNameWarningLabel"),
							"ChooseDest", JOptionPane.WARNING_MESSAGE);
			}

			else if (b == cc) {
				String name = ((Entry) list.getSelectedValue()).toString();

				if (name != null) {
					ccNames.add(name);
					ccList.setListData(ccNames);
				}

				else {
					JOptionPane.showMessageDialog(null, msgBundle
							.getString("ChooseDest.noNameWarningLabel"),
							"ChooseDest", JOptionPane.WARNING_MESSAGE);
				}
			}

			else if (b == bcc) {
				String name = ((Entry) list.getSelectedValue()).toString();

				if (name != null) {
					bccNames.add(name);
					bccList.setListData(bccNames);
				}

				else
					JOptionPane.showMessageDialog(null, msgBundle
							.getString("ChooseDest.noNameWarningLabel"),
							"ChooseDest", JOptionPane.WARNING_MESSAGE);
			}

			else if (b == ok) {
				int i;

				int size = toNames.size();

				if (size > 0) {
					toEmails = new String[size];

					String entry = null;

					for (i = 0; i < size; i++) {
						entry = (String) toNames.get(i);
						toEmails[i] = entry;
					}
				}

				size = ccNames.size();

				if (size > 0) {
					ccEmails = new String[size];

					String entry = null;

					for (i = 0; i < size; i++) {
						entry = (String) ccNames.get(i);
						ccEmails[i] = entry;
					}
				}

				size = bccNames.size();

				if (size > 0) {
					bccEmails = new String[size];

					String entry = null;

					for (i = 0; i < size; i++) {
						entry = (String) bccNames.get(i);
						bccEmails[i] = entry;
					}
				}

				hide();
			}

			else if (b == cancel)
				hide();
		}
	}
}
